public class Prefix {
	public long prefix = -1;
	public long len = -1;
	public int maxlen = -1;
	public static boolean local_debug_flag = true;
	public boolean equals(Object p1){
		
		//System.out.println("\n\n===========>  Bucket Comparison was called\n\n");
		
		Prefix p2 = (Prefix) p1;
		
		if (p2.len == this.len ){
			if (p2.maxlen != maxlen){
			//	System.out.println("ATTENTION: MAXLEN of these TWO prefixes is different:\n"+p2+" and "+this);
			}
			if ((p2.prefix >> (maxlen-len)) == (prefix >>(maxlen-len))){
				return true;
			}
		}
		//System.out.println("Prefix comparison: p1 = "+(Prefix)p1+"\n p2 = " + this);
		return false;
	}	
	public String toString(){
		if (prefix == -1 || len == -1){return "Nil";}
		String returnstring = "";
		for (int i = 0; i < maxlen - len; i++){
			returnstring += "*";
		}
		//return Long.toBinaryString(this.prefix>>(maxlen-len))+returnstring+"/"+ this.len;
		returnstring =  Long.toBinaryString(this.prefix>>(maxlen-len))+returnstring;
		if (returnstring.length() > maxlen){
			returnstring = returnstring.substring(returnstring.length()-maxlen);
		}else if (returnstring.length() < maxlen){
			int strlen = returnstring.length();
			for (int i = 0; i < maxlen - strlen; i++ ){
				returnstring = "0"+returnstring;
			}
		}
		return returnstring;
	}
	public int hashCode(){
		return (this+"").hashCode();
	}
	public int getMatchValue(long n1) {
		// how well it matches with the prefix, e.g. if the prefix is 01* and n1 = 011 then match is 1, 
		// similarly if the prfix is 101 and n1 = 011 then match is 0.
		int match = 0;
		int currentbit = maxlen; // it is the maxlen_th bit to be compared from the left.
		// so if the number is 1011 then maxlen = 4, and 4th bit from the left is 1, 3rd is 0 and so on.
		for (int i = 0; i < len; i++){
			if (getKthBit(n1,currentbit) == getKthBit(prefix,currentbit)){
				match++;
			}else{
				break;
				// current bit differs so return
			}
			currentbit--;
		}
		World.println("Prefix match value for prefix: " + this + " and Node id : " + World.toBinaryString(n1) + " is " + match , local_debug_flag);
		return match;
	}
	public static int getKthBit(long n, int k){
		//World.println(Long.toBinaryString(n), local_debug_flag);
		long mask = 1 << (k-1);
		//World.println(k+"th bit is: " + (int) (mask & n),local_debug_flag);
		return (int) (mask & n);
	}

}